package render;

public class RasterizerBase {
    
/*
    Coordinate system:
           o-------` +x
          /|
         / |
        /  |
    +z `   ` +y
*/
    
    protected int[] pixels;
    protected float[] zbuffer;
    public final int drawW;
    public final int drawH;
    public final int nPixels;
    
    public RasterizerBase(int imgWidth, int imgHeight, int[] pxs) {
        drawW = imgWidth;
        drawH = imgHeight;
        pixels = pxs;
        nPixels = pixels.length;
        zbuffer = new float[nPixels];
    }
    
    public void clear(int col) {
        for (int i = 0; i < nPixels; i++) {
            pixels[i] = col;
        }
    }
    
    public void clearZBuffer() {
        for (int i = 0; i < nPixels; i++) {
            zbuffer[i] = -1.0f;
        }
    }
    
    protected void pixel(int indPx, float zpos, int color) {
        zbuffer[indPx] = zpos;
        pixels[indPx] = color;
    }
    
    protected int getPixelIndex(int xi, int yi) {
        return (xi + drawW * yi);
    }
    
    protected boolean shouldPixelAt(int indPx, int x, float zpos) {
        return (indPx >= 0 && indPx < nPixels && x >= 0 && x < drawW && zpos >= zbuffer[indPx]);
    }
    
}
